package com.ruochen.recipe.customer.sectione;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruochen.common.adapter.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public abstract class SectionedExpandableLayoutHelper<SG extends SectionGroup, SC extends SectionChild> implements SectionStateChangeListener<SG> {

    //data list
    private LinkedHashMap<SG, List<SC>> mSectionDataMap = new LinkedHashMap<>();
    private List<Object> mDataArrayList = new ArrayList<>();

    //adapter
    private SectionedExpandableGridAdapter<SG, SC> mSectionedExpandableGridAdapter;

    //recycler view
    RecyclerView mRecyclerView;

    public SectionedExpandableLayoutHelper(Context context, RecyclerView recyclerView,
                                           int groupLayoutRes,
                                           int childLayoutRes,
                                           ItemClickListener<SG, SC> itemClickListener, int gridSpanCount) {
        //setting the recycler view
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, gridSpanCount);
        recyclerView.setLayoutManager(gridLayoutManager);
        mSectionedExpandableGridAdapter = new SectionedExpandableGridAdapter<SG, SC>(context, mDataArrayList, groupLayoutRes, childLayoutRes,
                gridLayoutManager, itemClickListener, this) {
            @Override
            public void convertGroup(RecyclerViewHolder holder, SG item, int position) {
                SectionedExpandableLayoutHelper.this.convertGroup(holder, item, position);
            }

            @Override
            public void convertChild(RecyclerViewHolder holder, SC item, int position) {
                SectionedExpandableLayoutHelper.this.convertChild(holder, item, position);
            }
        };
        recyclerView.setAdapter(mSectionedExpandableGridAdapter);
        mRecyclerView = recyclerView;
    }

    public abstract void convertGroup(RecyclerViewHolder holder, SG item, int position);

    public abstract void convertChild(RecyclerViewHolder holder, SC item, int position);

    public void notifyDataSetChanged() {
        //TODO : handle this condition such that these functions won't be called if the recycler view is on scroll
        generateDataList();
        mSectionedExpandableGridAdapter.notifyDataSetChanged();
    }

    private void generateDataList () {
        mDataArrayList.clear();
        for (Map.Entry<SG, List<SC>> entry : mSectionDataMap.entrySet()) {
            SG key;
            mDataArrayList.add((key = entry.getKey()));
            if (key.isExpanded())
                mDataArrayList.addAll(entry.getValue());
        }
    }

    @Override
    public void onSectionStateChanged(SG section, boolean isOpen) {
        if (!mRecyclerView.isComputingLayout()) {
            section.setExpanded(isOpen);
            notifyDataSetChanged();
        }
    }

}
